﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IGolovkine.AoA.BinarySerialization
{
    public class BinaryWriter : IBinaryWriter
    {
        private readonly System.IO.BinaryWriter _writer;
        public BinaryWriter(Stream stream)
        {
            _writer = new System.IO.BinaryWriter(stream);
        }

        public void WriteInt32(int value)
        {
            _writer.Write(value);
        }

        public void WriteString(string value)
        {
            _writer.Write(value);
        }

        public void WriteObject<T>(T obj) where T : IBinarySerializable
        {
            obj.Serialize(this);
        }

        public void WriteObjectArray(IBinarySerializable[] array)
        {
            TypeMap map = new TypeMap(array);
            map.Serialize(this);
            foreach (IBinarySerializable obj in array)
            {
                this.WriteObject(obj);
            }
        }

        public void WriteByteArray(byte[] array)
        {
            _writer.Write(array.Length);
            _writer.Write(array);
        }
    }
}
